<?php
require_once("../config/path_config.php");
require_once (ROOTDIR."config/sys_config.php");
require_once(ROOTDIR."admin/admin-header.php");
require_once(ROOTDIR."include/check_get_key.php");
require_once(ROOTDIR."common/db_utils.php");
require_once (ROOTDIR."common/utils.php");

$id=intval($_GET['id']);
$allowAccess = isset($_SESSION['administrator']);
if ( !$allowAccess ) {
    $sql = "select `creater_id` from problem where `problem_id`=?";
    $result = DbUtil::getInstance()->query($sql, $id);
    $allowAccess = isset($_SESSION['problem_editor'])
            && strtolower($result[0]['creater_id']) == strtolower($_SESSION['user_id']);
}

if (!$allowAccess ) {
    echo "<a href='../loginpage.php'>请登录或继续操作</a>";
    exit(1);
}

// 删除测试数据
$basedir = SysConfig::$OJ_DATA."/".intval($id);
if( strlen($basedir)>16 ) {
    deleteDir($basedir);
    //system("rm -rf $basedir");
}

$sql="delete FROM `problem` WHERE `problem_id`=?";
DbUtil::getInstance()->query($sql, $id);

// 更新table的自增起点值
$sql="select max(problem_id) FROM `problem`" ;
$result = DbUtil::getInstance()->query($sql);
$row = $result[0];
$max_id = $row[0];
$max_id++;
if( $max_id<1000 ) {
    $max_id = 1000;
}
$sql="ALTER TABLE problem AUTO_INCREMENT = $max_id";
$result = DbUtil::getInstance()->query($sql);

?>
<script language=javascript>
    history.go(-1);
</script>